package demo.java.b_cases;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class Demo05_forEach extends SimpleTagSupport{
	
	//循环标签，最少有两个属性，items   var
	private Object items;
	private String var;
		
	public void setItems(Object items) {
		this.items = items;
	}



	public void setVar(String var) {
		this.var = var;
	}



	@Override
	public void doTag() throws JspException, IOException {
		//判断是否是List集合
		if(items instanceof List){
			List list=(List)items;
			for(Object o:list){
				//将对象放入page域中
				this.getJspContext().setAttribute(var, o);
				//将标签体的内容输出
				this.getJspBody().invoke(null);
			}
		}
		//判断是否是map集合
		if(items instanceof Map){
			Map map=(Map)items;
			Set<Entry> entrys=map.entrySet();
			for(Entry entry:entrys){
				this.getJspContext().setAttribute(var,entry);
				this.getJspBody().invoke(null);
			}
			
		}
	}
}
